package com.azdebugit.java8.future;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;

public class Thread02_WhenComplete {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
            Boolean flag = true;
            if (Math.random() < 0.5) {
                flag = false;
                throw new RuntimeException("出错了");
            }
            System.out.println("正常结束");
            return flag;

        }).whenComplete(new BiConsumer<Boolean, Throwable>() {
            @Override
            public void accept(Boolean aDouble, Throwable throwable) {

                if (aDouble) {
                    System.out.println("whenComplete aDouble is null");
                } else {
                    System.out.println("whenComplete aDouble is " + aDouble);
                }

                if (throwable == null) {
                    System.out.println("whenComplete throwable is null");
                } else {
                    System.out.println("whenComplete throwable is " + throwable.getMessage());
                }
            }
        });

        System.out.println("结果 =00000000 ");
        System.out.println("最终返回的结果 = " + future.get());
    }
}
